package jp.melodroid.no6.timepicker;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import java.util.Calendar;
import java.lang.StringBuilder;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.widget.TimePicker;

public class HelloTimePicker extends Activity
{
    /* view */
    private TextView mTimeDisplay;
    private Button mPickTime;

    /* display values */
    private int mHour;
    private int mMinute;

    /* dialog ids */
    static final int TIME_DIALOG_ID = 0;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

	/* get views */
	mTimeDisplay = (TextView)findViewById(R.id.timeDisplay);
	mPickTime = (Button)findViewById(R.id.pickTime);

	/* set listener */
	mPickTime.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
		    showDialog(TIME_DIALOG_ID);
		}
	    });

	/* get current time */
	final Calendar c = Calendar.getInstance();
	mHour = c.get(Calendar.HOUR_OF_DAY);
	mMinute = c.get(Calendar.MINUTE);

	updateDisplay();

    }

    private void updateDisplay() {
	mTimeDisplay.setText(new StringBuilder().
			     append(mHour).append(":").append(mMinute) );
    }


    private TimePickerDialog.OnTimeSetListener mTimeSetListener = 
	new TimePickerDialog.OnTimeSetListener() {
	    public void onTimeSet(TimePicker v,int hour,int min) {
		mHour = hour;
		mMinute = min;
		updateDisplay();
	    }
	};

    protected Dialog onCreateDialog(int id, Bundle args) {
	switch(id) {
	case TIME_DIALOG_ID:
	    return new TimePickerDialog(this,
					mTimeSetListener, mHour,mMinute, false);
	default:
	    break;
	}
	return null;
    }

}
